package com.mzy.datepackage.M2022.Jun.twone;

import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/6/21 15:59
 * @Version 1.0
 */
public class test {
    private static final String serviceConfPath ="C:\\Users\\mzy\\Desktop\\service\\CHANNEL_UNIDIRECTIONAL_QHZ.json";
    @Test
     public void test(){
        String substring = serviceConfPath.substring(serviceConfPath.lastIndexOf("\\") + 1);
        String substring1 = serviceConfPath.substring(0, serviceConfPath.lastIndexOf("\\") + 1);
        System.out.println(substring1+"当前配置文件版本"+ File.separator + substring);
//        if (true){
//            return;
//        }

        System.out.println("xx");
        String s = substring1 + "当前配置文件版本" + File.separator + substring;
        File file = new File(s);
        System.out.println(file.getParentFile().getParentFile());
        System.out.println(file.getParentFile().getParent());
        String file2 = file.getParentFile().getParent()+ File.separator+ "当前配置文件版本" + File.separator + substring;
        System.out.println(file2);


        File parentFile = file.getParentFile();
        writeConf(parentFile.getPath(),"测试");
    }

    /**
     * 写入文件，通过加锁完成
     * */
    private void writeConf(String confPath,String data) {
        FileOutputStream outputStream = null;
        try {
            File confFile = new File(confPath);
            File dir = confFile.getParentFile();
            System.out.println(dir.getPath());
            if (!dir.isDirectory()){
                dir.mkdirs();
            }

            outputStream = new FileOutputStream(confPath);
            FileChannel channel = outputStream.getChannel();
            //将配置写入文件中
            outputStream.write(data.getBytes());
        } catch (Exception e) {
            System.out.println("??");
        }
        }


}
